#include <iostream>

using namespace std;

int Kesimo (int a[],  int pi, int pf, int k)
{
    int i,j;
    int x;
    if (pi<=pf)
    {
        x=a[pi];
        i=pi+1;
        j=pf;
        do
        {
            while (a[i]<x)
            {
                i++;
            }
            while (a[j]>x)
            {
                j--;
            }
            if (i<=j)
            {
                int s=a[i];
                a[i]=a[j];
                a[j]=s;
                i++;
                j--;
            }
            //~ cout << "X,I,J\t" << x << "\t" << i << "\t" << j << endl;
        }
        while (i<=j);
        //~ for(i=0; i<9; i++)cout << a[i]<< "\t"; cout << endl;
        int s=a[pi];
        a[pi]=a[j];
        a[j]=s;


        if(k<j)
        {
            return Kesimo (a,pi,j-1,k);
        }
        else if(k>j)
        {
            return Kesimo (a,j+1,pf,k);
        }
        else if(k==j)
        {
            for(i=0; i<9; i++)cout << a[i]<< "\t"; cout << endl;
            return(a[j]);
        }
    }
}

int main()
{
    for (int i=0; i<10; i++)
    {
        int a[]={11, 27 ,2 ,6 ,33 ,50, 7 ,4, 99, 1};
        cout << i << "\t" << Kesimo(a, 0, 10, i) << endl << endl;
    }
}
